package com.lhd.qd.module.system.dict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhd.qd.common.base.ApiResult;
import com.lhd.qd.common.base.BaseController;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.module.system.dict.model.dto.DictSaveDTO;
import com.lhd.qd.module.system.dict.model.dto.DictUpdateDTO;
import com.lhd.qd.module.system.dict.model.vo.DictCacheVO;
import com.lhd.qd.module.system.dict.model.vo.DictDetailVO;
import com.lhd.qd.module.system.dict.model.vo.DictListVO;
import com.lhd.qd.module.system.dict.service.DictCacheService;
import com.lhd.qd.module.system.dict.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "字典")
@RequestMapping("dict")
@RestController
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictCacheService dictCacheService;

    @ApiOperation(value = "分页列表", response = DictListVO.class)
    @GetMapping("type/{typeCode}")
    public ResponseEntity<ApiResult<IPage<DictListVO>>> pageDict(@PathVariable("typeCode") String typeCode,
                                                                 BasePageQuery query) {

        return success(dictService.pageDict(typeCode, query));
    }

    @ApiOperation(value = "详情", response = DictDetailVO.class)
    @GetMapping("{code}/type/{typeCode}")
    public ResponseEntity<ApiResult<DictDetailVO>> updateDictById(@PathVariable("typeCode") String typeCode,
                                                                  @PathVariable("code") Integer code) {

        return success(dictService.getDictById(typeCode, code));
    }

    @ApiOperation(value = "缓存", response = DictCacheVO.class)
    @PostMapping("cache")
    public ResponseEntity<ApiResult<Map<String, List<DictCacheVO>>>> dictCache(@RequestBody List<String> typeCodeList) {

        return success(dictCacheService.getDictCacheByTypeCodeList(typeCodeList));
    }

    @ApiOperation(value = "新增")
    @PostMapping("type/{typeCode}")
    public ResponseEntity<ApiResult> saveDict(@PathVariable("typeCode") String typeCode,
                                              @Validated @RequestBody DictSaveDTO saveDTO) {

        return successOrFail(dictService.saveDict(typeCode, saveDTO));
    }

    @ApiOperation(value = "修改")
    @PutMapping("{code}/type/{typeCode}")
    public ResponseEntity<ApiResult> updateDictById(@PathVariable("typeCode") String typeCode,
                                                    @PathVariable("code") Integer code,
                                                    @Validated @RequestBody DictUpdateDTO updateDTO) {

        return successOrFail(dictService.updateDictById(typeCode, code, updateDTO));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{code}/type/{typeCode}")
    public ResponseEntity<ApiResult> removeDictById(@PathVariable("typeCode") String typeCode,
                                                    @PathVariable("code") Integer code) {

        return successOrFail(dictService.removeDictById(typeCode, code));
    }
}
